﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.DTO;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Common;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class UploadFileModel : BaseModel
    {
        public string ClassData { get; set; }
        public string FileClassID { get; set; }
        public string FileUrl { get; set; }
        public string FileName { get; set; }
        public string Remark { get; set; }
        public int Sort { get; set; }
        public byte[] FData { get; set; }
        public int FLength { get; set; }
        public string FName { get; set; }
        public string FExtName { get; set; }
        public string FType { get; set; }
        public string Source { get; set; }
        public string ID { get; set; }
        public string FileID { get; set; }
        public string CallBack { get; set; }

        public UploadFileModel()
        {
        }
        public UploadFileModel(SessionEnt sessionEnt)
        {
            this.SessionEnt = sessionEnt;
        }


        public void InitData()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.FileType);
            ClassData = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
        }

        public void InitFile(string fileInfo)
        {
            SimpleFile file = Common.Utility.JsonSerialization.DeserializeObject<SimpleFile>(fileInfo);
            this.ID = file.ID;
            this.FileID = file.FileID;
            this.FileName = file.FName;
            this.Remark = file.Remark;
            this.Sort = file.Sort.Value;
        }

        public string SaveFile()
        {
            string id = Guid.NewGuid().ToString();
            var rp = new FileRepository();
            string _fileName = string.IsNullOrEmpty(FileName) ? FName : FileName;
            rp.SaveFile(new TP_SysFile
            {
                ID = id,
                CreateBy = SessionEnt.UserName,
                CreateTime = DateTime.Now,
                FClassID = FileClassID,
                FData = FData,
                Sort = Sort,
                Remark = Remark,
                FType = FType,
                FLength = FLength,
                FName = _fileName,
                FExtName = FExtName
            });
            SimpleFile fileInfo = new SimpleFile
            {
                ID = id,
                Remark = Remark,
                FName = _fileName,
                Sort = Sort
            };
            return id;
        }
        /// <summary>
        /// 更新文件
        /// </summary>
        /// <returns>文件字符串信息</returns>
        public string EditFile(bool newFile)
        {

            string _fileName = string.IsNullOrEmpty(FileName) ? FName : FileName;
            //如果是新的文件则记录到文件表里
            if (newFile)
            {
                var rp = new FileRepository();
                rp.SaveFile(new TP_SysFile
                {
                    CreateBy = SessionEnt.UserName,
                    CreateTime = DateTime.Now,
                    FClassID = FileClassID,
                    FData = FData,
                    Sort = Sort,
                    Remark = Remark,
                    FType = FType,
                    FLength = FLength,
                    FName = _fileName,
                    FExtName = FExtName
                });
            }
            SimpleFile fileInfo = new SimpleFile
            {
                ID = ID,
                Remark = Remark,
                FileID = FileID,
                FName = _fileName,
                Sort = Sort
            };
            return Common.Utility.JsonSerialization.SerializeObject(fileInfo);
        }
    }
}